﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Winform_Double_Buffer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        private bool flag = true;
        private Brush brush;

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime t1 = DateTime.Now;
            Graphics g = this.CreateGraphics();
            if (flag)
            {
                brush = new LinearGradientBrush(new PointF(0.0f, 0.0f), new PointF(700.0f, 300.0f), Color.Red, Color.Blue); flag = false;
            }
            else
            {
                brush = new LinearGradientBrush(new PointF(0.0f, 0.0f), new PointF(700.0f, 300.0f), Color.Blue, Color.Red); flag = true;
            }
            for (int j = 0; j < 60; j++)
            {
                for (int i = 0; i < 60; i++)
                {
                    g.FillEllipse(brush, i * 10, j * 10, 10, 10);
                }
            }
            DateTime t2 = DateTime.Now;
            TimeSpan sp = t2 - t1;
            float per = 1000 / sp.Milliseconds;
            this.label1.Text = "速度：" + per.ToString() + "帧/秒";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer2.Enabled = !timer2.Enabled;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            DateTime t1 = DateTime.Now;
            Bitmap bmp = new Bitmap(600, 600);
            Graphics g = Graphics.FromImage(bmp);
            if (flag)
            {
                brush = new LinearGradientBrush(new PointF(0.0f, 0.0f), new PointF(700.0f, 300.0f), Color.Red, Color.Blue); flag = false;
            }
            else
            {
                brush = new LinearGradientBrush(new PointF(0.0f, 0.0f), new PointF(700.0f, 300.0f), Color.Blue, Color.Red); flag = true;
            }
            for (int j = 0; j < 60; j++)
            {
                for (int i = 0; i < 60; i++)
                {
                    g.FillEllipse(brush, i * 10, j * 10, 10, 10);
                }
            }
            this.CreateGraphics().DrawImage(bmp, 0, 0);
            DateTime t2 = DateTime.Now;
            TimeSpan sp = t2 - t1;
            float per = 1000 / sp.Milliseconds;
            this.label1.Text = "速度：" + per.ToString() + "帧/秒";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MainForm form = new MainForm();
            form.Show();
        }
    }
}